package com.xj.driver.mqtt.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xj.common.bean.device.DeviceValue;
import com.xj.common.bean.point.PointValue;
import com.xj.common.model.Point;
import com.xj.common.sdk.bean.driver.DriverContext;
import com.xj.common.sdk.service.DriverService;
import com.xj.common.sdk.service.PointValueService;
import com.xj.driver.mqtt.service.UpBizService;
import com.xj.driver.mqtt.util.TimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author sunzhenzhen
 */
@Slf4j
@Service
public class UpBizServiceImpl implements UpBizService {
    @Resource
    private PointValueService pointValueService;

    @Resource
    private DriverService driverService;

    @Resource
    private DriverContext driverContext;

    @Override
    public Boolean doUpBiz(JSONArray payload) {
        List<PointValue> listPointV= new ArrayList<>();
        payload.forEach(device->{
            JSONObject jo = (JSONObject)device;
            JSONArray deviceArray =  jo.getJSONArray("services");
            deviceArray.forEach(device_service->{
                JSONObject service_jo=(JSONObject) device_service;
                JSONObject service_data = (JSONObject)service_jo.get("data");
                String d_id = (String)service_data.get("d_id");
                String deviceNo = d_id.split(":")[1];
                String time = (String)service_data.get("time");
                long deviceId = driverContext.getDriverMetadata().getDeviceSnMap().get(deviceNo).getId();
                Map d_data=(Map) service_data.get("d_data");
                /*查询是否有通讯终端数据*/
                DeviceValue deviceValue = doDeviceStatus(deviceId,d_data,time);
                if (deviceValue.getDeviceId()!=null){
                    driverService.sendDeviceStaus(deviceValue);
                    return;
                }
                /*遍历map*/
                d_data.keySet().forEach(data_key->{
                        String key = data_key.toString();
                        String value = String.valueOf(d_data.get(data_key));
                        listPointV.add(pointValueService.getPointValue(deviceId,key,value,false,""));
                    }
                );
            });
        });
        driverService.pointValueSender(listPointV);
        return true;
    }

    @NotNull
    private DeviceValue doDeviceStatus(long deviceId, Map d_data, String time) {
        DeviceValue deviceValue = new DeviceValue();
        d_data.keySet().forEach(data_key->{
            String key = data_key.toString();
            String value = String.valueOf(d_data.get(data_key));
            Point point = driverContext.getPointByDeviceIdAndPointName(deviceId,key);
            if (point.getType().equals("tinyint")){
//                Integer status =Integer.valueOf(value);
                deviceValue.setStatus(value);
                deviceValue.setDeviceId(deviceId);
                try {
                    long deviceTime =  TimeUtil.convertStringToLong(time,"yyyyMMddhhmmssSSS");
                    deviceValue.setTs(deviceTime);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                return;
            }
        });
        return deviceValue;
    }
}
